//
//  ListViewController.m
//  项目A --半糖
//
//  Created by romana on 16/5/19.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "ListViewController.h"
#import "AFNetworking.h"
#import "ListVCModel.h"
#import "ListCollectionViewCell.h"
#import "ListSecondDetailsViewController.h"
#import "BaseCollectionView.h"



#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height



@interface ListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>


@property(nonatomic,strong)NSMutableArray *myArray;
@property(nonatomic,strong)BaseCollectionView *myCollection;

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor whiteColor];
    
    self.myArray = [NSMutableArray array];
    [self getData];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //每个item的大小
    flowLayout.itemSize = CGSizeMake((WIDTH - 50) / 3, 120);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 30, 10);
    self.myCollection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49 - 44) collectionViewLayout:flowLayout];
 //   self.myCollection.pagingEnabled = YES;
    self.myCollection.showsHorizontalScrollIndicator = NO;
    self.myCollection.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myCollection];
    
    
    [self.myCollection registerClass:[ListCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    
    
    // Do any additional setup after loading the view.
}

-(void)getData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
     [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];

    NSDictionary *dic = @{@"app_id":@"com.jzyd.BanTang",@"app_installtime":@"1434705307",@"app_versions":@"5.7",@"channel_name":@"appStore",@"client_id":@"bt_app_ios",@"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5",@"os_versions":@"9.3.1",@"screensize":@"1242",@"track_device_info":@"iPhone7%2C1",@"track_deviceid":@"C2D3B460-4E0E-4639-A030-D274CFC43D50",@"v":@"12"};
    [manager POST:@"http://open3.bantangapp.com/category/scene" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
     //   NSLog(@"%@",responseObject);
        
        NSMutableDictionary *bigDic = responseObject;
        NSMutableArray *dataArray = [bigDic objectForKey:@"data"];
        for (NSDictionary *dic in dataArray) {
            
            ListVCModel *model = [[ListVCModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:model];
            
            NSLog(@"%@",self.myArray);
        }
        [self.myCollection reloadData];

        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];




}

//-(void)getData{
//
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//     [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
//    NSDictionary *dic = @{@"app_id":@"com.jzyd.BanTang",@"app_installtime":@"1434705307",@"app_versions":@"5.7",@"channel_name":@"appStore",@"client_id":@"bt_app_ios",@"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5",@"is_new":@"1",@"os_versions":@"9.3.1",@"screensize":@"1242",@"track_device_info":@"iPhone7%2C1",@"track_deviceid":@"C2D3B460-4E0E-4639-A030-D274CFC43D50",@"v":@"12"};
//    
//    [manager POST:@"http://open3.bantangapp.com/category/list" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        NSLog(@"%@",responseObject);
//        
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        
//    }];
//
//
//
//}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.myArray.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

  
    ListCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
  
    item.listModel = [self.myArray objectAtIndex:indexPath.row];
    // item.backgroundColor = [UIColor purpleColor];
    return  item;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ListSecondDetailsViewController *LSDVC = [[ListSecondDetailsViewController alloc] init];
    LSDVC.LVCModel = [self.myArray objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:LSDVC animated:YES];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
